﻿using ChuanYe.CountyFireManagement.EF.MySQL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class VideoAlarmBLL
    {
        #region 构造单例
        private VideoAlarmBLL() { }
        private static VideoAlarmBLL _instance;
        public static VideoAlarmBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new VideoAlarmBLL());
            }
        }
        #endregion

        public ResultDto<dynamic, dynamic> GetVideoAlarmByPage(string addressCounty,string addressTown,string addressVillage, RequestPageBase page)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var sql = " select a.*,b.VideoName,b.VideoPath,c.LocaName,c.AddressCounty,c.AddressTown,c.AddressVillage from videoalarm as a ";
                sql += " left join locavideo as b on a.nUnit = b.nUnit ";
                sql += " left join locainfo as c on a.LocaID =c.LocaID ";
                sql += " where 1=1 ";
                if (!string.IsNullOrEmpty(addressCounty))
                {
                    sql += " and c.AddressCounty = '{0}'";
                    sql = string.Format(sql, addressCounty);
                }
                if (!string.IsNullOrEmpty(addressTown))
                {
                    sql += " and  c.AddressTown = '{0}'";
                    sql = string.Format(sql, addressTown);
                }
                if (!string.IsNullOrEmpty(addressVillage))
                {
                    sql += " and c.AddressVillage = '{0}'";
                    sql = string.Format(sql, addressVillage);
                }

                if (!string.IsNullOrEmpty(page.Keyword))
                {
                    sql += " and c.LocaName like '%{0}%'";
                    sql = string.Format(sql, page.Keyword);
                }

                sql += page.OrderByPattern == 0 ? "a.AlarmTime".OrderByDescending() : "a.AlarmTime".OrderBy();
                sql += page.Limit();
                var list = ef.Database.SqlQuery<ChuanYe.CountyFireManagement.Model.VideoAlarmModel>(sql);
                var countSql = sql.CountSql();
                var count = ef.Database.SqlQuery(countSql).Rows[0][0];
                result.FirstParam = list;
                result.SecondParam = count;
                return result;
            }
        }

        public int ChangeAuditState(string alarmId, int auditState)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Videoalarm.Where(p => p.AlarmId == alarmId ).FirstOrDefault();
                if (exist == null)
                {
                    return -1;
                }
                exist.AuditState = auditState;
                ef.Videoalarm.Add(exist);
                ef.Entry<Videoalarm>(exist).State = EntityState.Modified;
                ef.SaveChanges();
                return 1;
            }
        }

        public int AddAlarmIgnoreTime(Alarmignoretime info)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var exist = ef.Alarmignoretime.Where(p => p.LocaId == info.LocaId && p.UserId==info.UserId).FirstOrDefault();
                if (exist != null)
                {
                    exist.UserId = info.UserId;
                    exist.IgnoreTime = info.IgnoreTime;
                    exist.Value = info.Value;
                    ef.Alarmignoretime.Add(exist);
                    ef.Entry<Alarmignoretime>(exist).State = EntityState.Modified;
                    ef.SaveChanges();
                }
                else
                {
                    ef.Alarmignoretime.Add(info);
                    ef.Entry<Alarmignoretime>(info).State = EntityState.Added;
                    ef.SaveChanges();
                }
                return 1;
            }
        }

        public List<dynamic> GetAlarmIgnoreTime(int userId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                return ef.Alarmignoretime.Where(p => p.UserId == userId).ToList()
                         .Select(s => new {  IgnoreId =  s.IgnoreId,
                                             LocaId = s.LocaId,
                                             IgnoreTime = s.IgnoreTime,
                                             Value = s.Value,
                                             AlarmNextTime=s.IgnoreTime.AddMinutes(s.Value)
                         })
                         .ToList<dynamic>();
            }
        }
    }
}
